// pages/footprint/footprint.js
const api = require('../../utils/util.js')
const service = require('../../utils/service.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listNull: true,
    list: [],
    num: '', //足迹数量
    checkAll: false,
    is_show: 2, //1显示 2隐藏
    edit_stat: [], //true显示 false隐藏
    del_ids: [],  //需要删除的足迹id
    delAllArr: [],  //储存全部删除的足迹ID
  },

  //编辑
  edit: function(e) {
    this.setData({
      is_show: 1,
    });
  },
  
  //单选
  singleClick: function(et) {
    let el = et.currentTarget;
    if (this.data.is_show == 2) {
      let goods_id = el.dataset.goodsId; // - 会转成驼峰写法
      wx.navigateTo({
        url: '../goods_detail/goods_detail?goods_id=' + goods_id,
      })
    } else if (this.data.is_show == 1) {
      let contentArr = this.data.edit_stat;
      let num = 0;
      let index1 = el.dataset.index;
      let index2 = el.dataset.index2;
      let step_id = el.dataset.stepId;
      let del_ids = this.data.del_ids;

      if (contentArr[index1][index2] == true) {
        //判断是否最后一个取消打勾
        for (let i = 0; i < contentArr.length; i++) {
          for (let j = 0; j < contentArr[i].length; j++) {
            if (this.data.edit_stat[i][j] == true) {
              num++;
            }
          }
        }

        let index = del_ids.indexOf(step_id);
        del_ids.splice(index, 1); //从删除数组中移除

        if (num == 1) {
          this.setData({
            [`edit_stat[${index1}][${index2}]`]: false,
            checkAll: false
          });
        } else {
          this.setData({
            [`edit_stat[${index1}][${index2}]`]: false
          });
        }
      } else {
        //判断是否最后一个显示打勾
        for (let i = 0; i < contentArr.length; i++) {
          for (let j = 0; j < contentArr[i].length; j++) {
            if (this.data.edit_stat[i][j] == false) {
              num++;
            }
          }
        }

        del_ids.push(step_id); //加入删除数组
        
        if (num == 1) {
          this.setData({
            [`edit_stat[${index1}][${index2}]`]: true,
            del_ids: del_ids,
            checkAll: true,
          });
        } else {
          this.setData({
            [`edit_stat[${index1}][${index2}]`]: true,
            del_ids: del_ids,
          });
        }
        console.log(JSON.stringify(this.data.edit_stat));
      }
    }

  },

  //全选
  checkAll: function(e) {
    let contentArr = this.data.edit_stat;
    if (!this.data.checkAll) {
      for (let i = 0; i < contentArr.length; i++) {
        for (let j = 0; j < contentArr[i].length; j++) {
          
          this.setData({
            [`edit_stat[${i}][${j}]`]: true,
            checkAll: true,
            del_ids: this.data.delAllArr
          });
        }
      }
    } else {
      for (let i = 0; i < contentArr.length; i++) {
        for (let j = 0; j < contentArr[i].length; j++) {
          this.setData({
            [`edit_stat[${i}][${j}]`]: false,
            checkAll: false,
            del_ids: []
          });
        }
      }
    }
  },

  reset:function(){
    this.setData({
      is_show: 2
    });
    // for (let i = 0; i < contentArr.length; i++) {
    //   for (let j = 0; j < contentArr[i].length; j++) {
    //     this.setData({
    //       [`edit_stat[${i}][${j}]`]: false,
    //       checkAll: false,
    //       is_show: 2
    //     });
    //   }
    // }
  },

  del: function(){
    if (this.data.del_ids.length < 1){
      api.toast('请至少选择一项...')
    }else{
      let step_id = this.data.del_ids.join(',');
      api.ajax(service.url('delFootPrint'), {
        step_id: step_id 
      }, function(res){
        console.log('删除成功：'+JSON.stringify(res))
        if (getCurrentPages().length != 0) {
          //刷新当前页面的数据
          getCurrentPages()[getCurrentPages().length - 1].onLoad()
        }
      });
    }
  },
  



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let _this = this;
    api.ajax(service.url('getFootPrint'), {
      page: 1
    }, function(res) {
      console.log(res)
      if(res.data.length){
        _this.data.listNull = false;
      }else{
        _this.data.listNull = true;
      }
      let num = 0;
      let contentArr = [];
      let delAllArr = [];
      for (let i = 0; i < res.data.length; i++) {
        contentArr[i] = new Array(i);
        for (let j = 0; j < res.data[i].list.length; j++) {
          num++;
          contentArr[i][j] = false;
          delAllArr.push(res.data[i].list[j].step_id);
        }
      }
      console.log('所有的足迹id: '+delAllArr)
      _this.setData({
        listNull: _this.data.listNull,
        list: res.data,
        num: num,
        edit_stat: contentArr,
        delAllArr: delAllArr
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})